#include "StdAfx.h"
#include "XmlSettings.h"

XmlSettings::XmlSettings(void)
{
	document = gcnew XmlDocument();
}

XmlSettings::XmlSettings(String^ _xml)
{
	document = gcnew XmlDocument();

	if (_xml=="")
	  document->LoadXml(DefaultXmlSettings());
	else
	  document->LoadXml(_xml);
}

String^ XmlSettings::DefaultXmlSettings()
{
	return gcnew String("<Usuwacz><Dir>C:\\</Dir><IfSubDir>1</IfSubDir><FileList></FileList><IfDirDel>1</IfDirDel></Usuwacz>");
}

String^ XmlSettings::getByChildName(String^ _childName)
{			
	XPathNavigator^ navigator = document->CreateNavigator();		

	navigator->MoveToChild("Usuwacz", "");
	if (!navigator->MoveToChild(_childName, ""))return gcnew String("");		
	
	return gcnew String(navigator->InnerXml);
}

String^ XmlSettings::getDir()
{
	return getByChildName("Dir");
}

bool XmlSettings::getIfSubDir()
{	
	String^ tekst = getByChildName("IfSubDir");
	if (tekst=="1")
		return true;

	return false;
}

bool XmlSettings::getIfDirDel()
{	
	String^ tekst = getByChildName("IfDirDel");
	if (tekst=="1")
		return true;

	return false;
}

void XmlSettings::getFileList(List<String^>^ _lista)
{	
	XPathNavigator^ navigator = document->CreateNavigator();
	
	navigator->MoveToChild("Usuwacz", "");
	if (!navigator->MoveToChild("FileList", ""))return;	

	_lista->Clear();	
	if (!navigator->MoveToChild("File", ""))return;	

	do
	{
		_lista->Add(navigator->InnerXml);
	}while(navigator->MoveToNext());	
}

String^ XmlSettings::getSettingsXml()
{
	return gcnew String(document->OuterXml);
}

void XmlSettings::setByChildName(String^ _childName, String^ _value)
{
	XPathNavigator^ navigator = document->CreateNavigator();
	navigator->MoveToChild("Usuwacz", "");
	if (!navigator->MoveToChild(_childName, ""))
	{
		navigator->InnerXml += "<"+_childName+"></"+_childName+">";
		if (!navigator->MoveToChild(_childName, ""))return;
	}

	navigator->InnerXml = _value;
}

void XmlSettings::setDir(String^ _dir)
{
	setByChildName("Dir", _dir);
}

void XmlSettings::setIfSubDir(bool _if_subdir)
{
	if (_if_subdir)
		setByChildName("IfSubDir", "1");
	else
		setByChildName("IfSubDir", "0");
}

void XmlSettings::setIfDirDel(bool _if_dirdel)
{
	if (_if_dirdel)
		setByChildName("IfDirDel", "1");
	else
		setByChildName("IfDirDel", "0");
}

void XmlSettings::setFileList(List<String^>^ _lista)
{
	XPathNavigator^ navigator = document->CreateNavigator();
	navigator->MoveToChild("Usuwacz", "");
	if (!navigator->MoveToChild("FileList", ""))return;

	String^ xml = gcnew String("");
	for (int i=0; i<_lista->Count; ++i)
	{
		xml+= "<File>"+_lista[i]+"</File>";
	}

	navigator->InnerXml = xml;
}